"""
Case Type   : 可靠性-数据库系统类
Case Name   : gs_ctl工具log目录修改权限
Create At   : 2020/07/20
Owner       : @li-xin12345
Description :
    step1: 修改gs_ctl日志文件权限为0
    step2: stop集群
    step3: 恢复gs_ctl日志文件权限
    step4: stop集群
    step5: 修改gs_ctl日志文件权限为0
    step6: start集群
    step7: 恢复gs_ctl日志文件权限
    step8: start集群
Expect      :
    step1: 修改gs_ctl日志文件权限为0； expect:成功
    step2: stop集群； expect:失败
    step3: 恢复gs_ctl日志文件权限； expect:成功
    step4: stop集群； expect:成功
    step5: 修改gs_ctl日志文件权限为0； expect:成功
    step6: start集群； expect:失败
    step7: 恢复gs_ctl日志文件权限； expect:成功
    step8: start集群； expect:成功
History     :
    Modified by peilinqian 2024-8-13：研发代码变更，gs_ctl日志权限会影响启动停止
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Deletaduit(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(f"----{os.path.basename(__file__)} start----")
        self.rootNode = Node()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_root = Node(node='PrimaryRoot')
        self.constant = Constant()
        self.log.info(os.path.dirname(macro.PG_LOG_PATH))
        self.ctl_log_path = os.path.join(os.path.dirname(macro.PG_LOG_PATH),
                                         'bin/gs_ctl')

    def test_main(self):
        text = '----step1: 修改gs_ctl日志文件权限为0； expect:成功----'
        self.log.info(text)
        chmod_cmd = f'chmod 000 {self.ctl_log_path}/*'
        self.log.info(chmod_cmd)
        chmod_msg = self.pri_root.sh(chmod_cmd).result()
        self.log.info(chmod_msg)
        self.assertEqual('', chmod_msg, '执行失败' + text)

        text = '----step2: stop集群； expect:失败----'
        self.log.info(text)
        stop_result = self.pri_sh.stopDbCluster(get_detail=True)
        self.assertNotIn(self.constant.STOP_SUCCESS_MSG, stop_result,
                         '执行失败' + text)
        self.assertIn("Permission denied", stop_result, '执行失败' + text)

        text = '----step3: 恢复gs_ctl日志文件权限； expect:成功----'
        self.log.info(text)
        chmod_cmd = f'chmod 600 {self.ctl_log_path}/*'
        self.log.info(chmod_cmd)
        chmod_msg = self.pri_root.sh(chmod_cmd).result()
        self.log.info(chmod_msg)
        self.assertEqual('', chmod_msg, '执行失败' + text)

        text = '----step4: stop集群； expect:成功----'
        self.log.info(text)
        stop_result = self.pri_sh.stopDbCluster()
        self.assertTrue(stop_result, '执行失败' + text)

        text = '----step5: 修改gs_ctl日志文件权限为0； expect:成功----'
        self.log.info(text)
        chmod_cmd = f'chmod 000 {self.ctl_log_path}/*'
        self.log.info(chmod_cmd)
        chmod_msg = self.pri_root.sh(chmod_cmd).result()
        self.log.info(chmod_msg)
        self.assertEqual('', chmod_msg, '执行失败' + text)

        text = '----step6: start集群； expect:失败----'
        self.log.info(text)
        start_result = self.pri_sh.startDbCluster()
        self.assertFalse(start_result, '执行失败' + text)

        text = '----step7: 恢复gs_ctl日志文件权限； expect:成功----'
        self.log.info(text)
        chmod_cmd = f'chmod 600 {self.ctl_log_path}/*'
        self.log.info(chmod_cmd)
        chmod_msg = self.pri_root.sh(chmod_cmd).result()
        self.log.info(chmod_msg)
        self.assertEqual('', chmod_msg, '执行失败' + text)

        text = '----step8: start集群； expect:成功----'
        self.log.info(text)
        start_result = self.pri_sh.startDbCluster()
        self.assertTrue(start_result, '执行失败' + text)

    def tearDown(self):
        self.log.info("----this is tearDown----")
        text = '----集群状态验证;expect: 正常----'
        self.log.info(text)
        self.status = self.pri_sh.getDbClusterStatus("status")
        if not self.status:
            step_txt = '----集群状态异常时，恢复环境----'
            self.log.info(step_txt)
            self.log.info("----恢复文件权限----")
            chmod_cmd = 'chmod 600 ' + self.ctl_log_path + '/*'
            self.log.info(chmod_cmd)
            result = self.pri_root.sh(chmod_cmd).result()
            self.log.info(result)
            self.pri_sh.startDbCluster()
            self.status = self.pri_sh.getDbClusterStatus("status")
        self.log.info(f"----{os.path.basename(__file__)} end----")
        self.assertTrue(self.status, '执行失败' + text)
